using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Staff
{
    [Authorize]
    public class OperatorAppService : BasicArchivesAppService, IOperatorAppService
    {
        protected IOperatorRepository OperatorRepository => LazyServiceProvider.LazyGetRequiredService<IOperatorRepository>();
        protected OperatorManager OperatorManager => LazyServiceProvider.LazyGetRequiredService<OperatorManager>();

        [Authorize(BasicArchivesPermissions.Operator.Query)]
        public async Task<OperatorDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<Operator, OperatorDto>(
                await OperatorRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.Operator.Query)]
        public async Task<OperatorDto> FindAsync(string filter)
        {
            var entity = await OperatorRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<Operator, OperatorDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Operator.Query)]
        public async Task<OperatorDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await OperatorRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<Operator, OperatorDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Operator.Query)]
        public async Task<PagedResultDto<OperatorDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await OperatorRepository.GetCountAsync(input.Filter, true);
            var list = await OperatorRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<OperatorDto>(
                totalCount,
                ObjectMapper.Map<List<Operator>, List<OperatorDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Operator.Query)]
        public async Task<PagedResultDto<OperatorDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<Operator, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<Operator>(input.FilterGroup);
            }

            var totalCount = await OperatorRepository.GetCountAsync(expression, true);
            var list = await OperatorRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<OperatorDto>(
                totalCount,
                ObjectMapper.Map<List<Operator>, List<OperatorDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Operator.Create)]
        public async Task<OperatorDto> CreateAsync(OperatorCreateDto input)
        {
            var entity = ObjectMapper.Map<OperatorCreateDto, Operator>(input);

            await OperatorManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Operator.Update)]
        public async Task<OperatorDto> UpdateAsync(Guid id, OperatorUpdateDto input)
        {
            var entity = await OperatorRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await OperatorManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Operator.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await OperatorManager.DeleteAsync(id);
        }
    }
}